<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
    	// Returns the name of a given category in the database
    	function getCategoryName($cid) {
			$db = new Connect();

			$sql = "SELECT cName FROM Category WHERE cid = " . $cid;
			
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0)
			{
				return "";
			}
			else
			{
				$rows = $db->to_array($result);
				return $rows[0]['cName'];
			}
    	}
    	
    	// Returns the search results from the postings table
    	function getPostingSearchResults() {
			$db = new Connect();

			$sql = "SELECT Posting.*, Category.cName, " .
				   "IFNULL(Posting.pCity, MemberInfo.mCity) AS city " .
				   "FROM Posting INNER JOIN Category ON Category.cID = Posting.cID " .
				   "INNER JOIN MemberInfo ON MemberInfo.mID = Posting.mID ";
			
			if (!empty($_GET["search"]))			
			{
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE (Posting.pName LIKE '%" . $_GET["search"] . "%' OR Posting.pDesc LIKE '%" . $_GET["search"] . "%')";
				else
					$sql = $sql . " AND (Posting.pName LIKE '%" . $_GET["search"] . "%' OR Posting.pDesc LIKE '%" . $_GET["search"] . "%')";
			}
			if (!empty($_GET["cid"]))
			{
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE Posting.cid = " . $_GET["cid"];
				else
					$sql = $sql . " AND Posting.cid = " . $_GET["cid"];
			}
			if (!empty($_GET["type"]))
			{
				$status = "'Open', 'Closed'";
				if ($_GET["type"] == "barter")
					$status = "'Barter', 'Traded'";
					
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE Posting.status IN(" . $status . ")";
				else
					$sql = $sql . " AND Posting.status IN(" . $status . ")";
			}
			if (!empty($_GET["mid"]))
			{
				if (strpos($sql, 'WHERE') === false)				
					$sql = $sql . " WHERE Posting.mID = " . $_GET["mid"];
				else
					$sql = $sql . " AND Posting.mID = " . $_GET["mid"];
			}
			if (!empty($_GET["city"]))			
			{
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE IFNULL(Posting.pCity, MemberInfo.mCity) = '" . $_GET["city"] . "'";
				else
					$sql = $sql . " AND IFNULL(Posting.pCity, MemberInfo.mCity) = '" . $_GET["city"] . "'";
			}
			if (!empty($_GET["minprice"]) && isset($_GET["maxprice"]))
			{
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE price between " . $_GET["minprice"] . " and " . $_GET["maxprice"];
				else
					$sql = $sql . " AND price between " . $_GET["minprice"] . " and " . $_GET["maxprice"];				
			}
			else if (!empty($_GET["minprice"]))
			{
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE price >= " . $_GET["minprice"];
				else
					$sql = $sql . " AND price >= " . $_GET["minprice"];
			}
			else if (!empty($_GET["maxprice"]))
			{
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE price <= " . $_GET["maxprice"];
				else
					$sql = $sql . " AND price <= " . $_GET["maxprice"];
			}
			if (!empty($_GET["mindate"]) && isset($_GET["maxdate"]))
			{
				if (strpos($sql, 'WHERE') === false)
					$sql = $sql . " WHERE pDatetime between '" . $_GET["mindate"] . "' and '" . $_GET["maxdate"] . " 23:59:59'";
				else
					$sql = $sql . " AND pDatetime between '" . $_GET["mindate"] . "' and '" . $_GET["maxdate"] . " 23:59:59'";
			}

			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0)
			{
				return;
			}
			else
			{
				$rows = $db->to_array($result);
				return $rows;
			}
    	}
    ?>
    <div id="main">
    	<p class="title">
    		Postings
    	</p>
<?php
	print "<p class=\"normal\">Search results for:<ul>";
	if (!empty($_GET["search"]))
		print "<li class=\"normal\">Keyword '" . $_GET["search"] . "'</li>";
	if (!empty($_GET["cid"]))
		print "<li class=\"normal\">Category '" . getCategoryName($_GET["cid"]) . "'</li>";
	if (!empty($_GET["city"]))
		print "<li class=\"normal\">City '" . $_GET["city"] . "'</li>";
	if (!empty($_GET["minprice"]) && isset($_GET["maxprice"]))
		print "<li class=\"normal\">Price between " . $_GET["minprice"] . "$ and " . $_GET["maxprice"] . "$</li>";
	else if (!empty($_GET["minprice"]))
		print "<li class=\"normal\">Price from " . $_GET["minprice"] . "$</li>";
	else if (!empty($_GET["maxprice"]))
		print "<li class=\"normal\">Price up to " . $_GET["maxprice"] . "$</li>";
	if (!empty($_GET["mindate"]) && isset($_GET["maxdate"]))
		print "<li class=\"normal\">Posted between " . $_GET["mindate"] . " and " . $_GET["maxdate"] . "</li>";
	if (!empty($_GET["mid"]))
		print "<li class=\"normal\">Your postings</li>";
	print "</ul></p>";
		
	$rows = getPostingSearchResults();
	
	if (!isset($rows))
	{
		print "No results found.";
	}
	else
	{
		print "<table border=\"0\">";
		print "<col width=\"120px\">";
		print "<col width=\"100px\">";
		print "<col width=\"200px\">";
		 
		$rowcount = count($rows);
		for ($i = 0; $i < $rowcount; $i++)
		{
			print "<tr>";
			print "<td colspan=\"3\" class=\"normal\"><a href=\"posting.php?pid=" . $rows[$i]['pID'] . "\">" . $rows[$i]['pName'] . "</a></td>";
			print "</tr>";
			print "<tr>";
			print "<td rowspan=\"4\" class=\"normal\">";
			print "<a href=\"posting.php?pid=" . $rows[$i]['pID'] . "\">";
			print "<img src=\"" . $rows[$i]['img'] . "\" height=\"100px\" width=\"100px\" alt=\"Image\"/></a></td>";
			print "</tr>";
			print "<tr>";
			print "<td class=\"normal\">" . $rows[$i]['cName'] . "</td>";
			print "<td class=\"normal\">" . $rows[$i]['pDatetime'] . "</td>";
			print "</tr>";
			print "<tr>";
			print "<td class=\"normal\">" . $rows[$i]['price'] . "$</td>";
			print "<td class=\"normal\">" . $rows[$i]['city'] . "</td>";
			print "</tr>";
			print "<tr>";
			print "<td class=\"normal\">&nbsp</td>";
			print "</tr>";
		}
		
		print "</table>";
	}
?>
    </div>
</body>
</html>